package org.ling.xuan.util;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UrlEntity {
    /**
     * 基础url
     */
    public String baseUrl;
    /**
     * url参数
     */
    public Map<String, Object> params = new HashMap<>();

    public String toUrl() {
        int i = 0;
        String text = baseUrl;
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (i == 0) {
                text = text + "?" + entry.getKey() + "=" + entry.getValue();
            } else {
                text = text + "&" + entry.getKey() + "=" + entry.getValue();
            }
            i++;
        }
        return text;
    }

    public UrlEntity addParam(Map<String, Object> map) {
        if (map != null) {
            params.putAll(map);
        }
        return this;
    }
}

